//
//  CRootCtrlManager.m
//  HCHK_iphone
//
//  Created by yilong zhang on 11-12-7.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "CRootCtrlManager.h"

@implementation CRootCtrlManager

static CRootCtrlManager *g_RootCtrl = nil;

@synthesize m_OverView;
@synthesize m_CurPage;
@synthesize m_NextPage;
@synthesize m_nImageNo;

@synthesize m_TabBar;
//@synthesize m_TabView;
//@synthesize m_TopBar;

@synthesize m_LoadView;
@synthesize m_LoadBg;
@synthesize m_LoadTitle;
@synthesize m_LoadIndicator;

@synthesize m_Ctrls;
@synthesize m_bIsActive;
//@synthesize m_OldView;

@synthesize m_TbFocus;
@synthesize m_PreViewBtn;
@synthesize m_UploadBtn;
@synthesize m_FavoriteBtn;

//share
@synthesize m_Share;

//commentary
@synthesize m_ComEdit;

//imageselector
@synthesize m_ImageSelector;

//alterview
@synthesize m_bIsAlterShow;
@synthesize m_AlterView;

//new
@synthesize m_enAppLaunchType;
@synthesize m_CitySel;

//cover
@synthesize m_Cover;
@synthesize m_Animas;
@synthesize m_bIsOver;

+(id)allocWithZone:(NSZone *)zone
{
    @synchronized(self)
    {
        if (!g_RootCtrl)
        {
            g_RootCtrl = [super allocWithZone:zone];
        }
    }
    
    return g_RootCtrl;
}

+(CRootCtrlManager*)DefaultRootCtrl
{
    if (!g_RootCtrl)
    {
        g_RootCtrl = [[CRootCtrlManager alloc] init];
    }
    
    return g_RootCtrl;
}

+(void)DestroyRootCtrl
{
    if (g_RootCtrl)
    {
        [g_RootCtrl release];
        g_RootCtrl = nil;
    }
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [m_LoadIndicator startAnimating];
    m_bIsActive = NO;
    m_TbFocus.hidden = NO;
    m_AlterView = nil;
    m_bIsAlterShow = NO;
    m_enAppLaunchType = Normal;
    
    m_nImageNo = (arc4random() % 10) + 1;
    //[CRootCtrlManager DefaultRootCtrl].m_CurPage.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", m_nImageNo]];
    //[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(ChangeCover) userInfo:nil repeats:NO];
    m_Cover = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    m_Cover.image = [UIImage imageNamed:@"background.png"];
    [m_OverView addSubview:m_Cover];
    [m_Cover release];
    
    m_Animas = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    m_Animas.image = [UIImage imageNamed:@"40.png"];
    [m_OverView addSubview:m_Animas];
    [m_Animas release];
    m_bIsOver = NO;
    
    [self performSelector:@selector(AnimationOver) withObject:nil afterDelay:6];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(DidAppBecameActive:) name:MSG_APP_ACTIVE object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(DidAppEnterBackground:) name:MSG_APP_BACKUP object:nil];
}

-(void)viewDidAppear:(BOOL)animated
{
    if (m_Animas.image)
    {
        NSMutableArray *imgs = [NSMutableArray array];
        for (int i=1; i<=40; i++)
        {
            [imgs addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png", i]]];
        }
        m_Animas.animationImages = imgs;
        m_Animas.animationRepeatCount = 1; 
        m_Animas.animationDuration = 5.0f; 
        [m_Animas startAnimating];
    }
}

-(void)dealloc
{
    [m_Ctrls release];
    
    [super dealloc];
}

//network
-(void)NetWorkRun:(NSNotification*)notification
{
    if (!m_bIsActive)
    {
        //启动页面网络状态恢复
        NSNumber *ret = notification.object;
        if ([ret boolValue])
        {
            //NSLog(@"未进入主页，监听网络状态。网络可用。");
            
            //无定位启动,直接加载数据
            if (NormalSupport != [[CLocator DefaultLocator] StartLocation])
            {
                [self LoadDatas];
            }
            else 
            {
                //监听定位
                [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(LocationReponse:) name:MSG_LOCATION_RET object:nil];
                [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(LocationReponse:) name:MSG_LOCATION_TIMEOUT object:nil];
            }
        }
        else 
        {
            //NSLog(@"未进入主页，监听网络状态。网络不可用。");
        }
    }
}

//location
-(void)LocationReponse:(NSNotification*)notification
{
    //移除对定位的监听
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MSG_LOCATION_TIMEOUT object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MSG_LOCATION_RET object:nil];
    
    CAddrInfo *code = notification.object;
    
    //定位失败
    if (!code)
    {
        //程序未启动
        if (!m_bIsActive)
        {
            UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请确认定位已经打开，确保较好地使用。" delegate:self cancelButtonTitle:@"退出" otherButtonTitles:@"继续", nil];
            alter.tag = 1;
            [alter show];
            [alter autorelease];
        }
    }
    else 
    {
        //用户位置发生变化，清空缓存,启动页面
        if (!m_bIsActive)
        {
            //BOOL bFlag = NO;
            //CAddrInfo *info = [CLocator DefaultLocator].m_AddrInfo;
            //if ((![[CUser DefaultUser].U_CurCity isEqualToString:info.m_strCity]) /*&& (Logined == [CUser DefaultUser].m_UType)*/)
            //{
                //bFlag = YES;
            //}
            
            //设置用户数据缓存
            [self SetUserData:YES];
            //开始加载主页数据
            [self performSelector:@selector(LoadDatas) withObject:self afterDelay:1];
        }
        //已经进入主页并且可视，执行刷新
        else 
        {
            //从后台恢复到前台，更新用户自己的地理xinxi
            [self SetUserData:NO];
            [self FreshMainPage];
        }
    }
}

-(void)FreshMainPage
{
    if ([CMainPageCtrl DefaultMainPageCtrl].m_TabView.m_Tab.m_nFocus <= 2 && m_bIsActive &&
        ![CMainPageCtrl DefaultMainPageCtrl].m_bIsFresh && ![CMainPageCtrl DefaultMainPageCtrl].m_bIsHidden && (0 == [CMainPageCtrl DefaultMainPageCtrl].m_nModel))
    {
        [[CMainPageCtrl DefaultMainPageCtrl] FreshMianPage:YES];
    }
}

-(void)SetUserData:(BOOL)isrun
{
    //设置用户当前经纬度
    CUser *user = [CUser DefaultUser];
    user.m_SelfPosition = CLLocationCoordinate2DMake([CLocator DefaultLocator].m_Coordinate2D.latitude, [CLocator DefaultLocator].m_Coordinate2D.longitude);
    
    CAddrInfo *info = [CLocator DefaultLocator].m_AddrInfo;
    user.m_AddrInfo.m_strCity    = [NSString stringWithFormat:@"%@",info.m_strCity];
    user.m_AddrInfo.m_strCountry = [NSString stringWithFormat:@"%@",info.m_strCountry];
    user.m_AddrInfo.m_strProvince= [NSString stringWithFormat:@"%@",info.m_strProvince];
    user.m_AddrInfo.m_strSum     = [NSString stringWithFormat:@"%@",info.m_strSum];
    user.m_AddrInfo.m_fX         = user.m_SelfPosition.latitude;
    user.m_AddrInfo.m_fY         = user.m_SelfPosition.longitude;
    
    NSLog(@"\n\r\n\rAddress of User:City:%@ Province:%@ Country:%@ Sum:%@ Latitude:%f Longtitude:%f\n\r\n\r", user.m_AddrInfo.m_strCity,user.m_AddrInfo.m_strProvince,user.m_AddrInfo.m_strCountry,user.m_AddrInfo.m_strSum,user.m_AddrInfo.m_fX,user.m_AddrInfo.m_fY);
    
    //clear buffer
    if (isrun)
    {
        ////NSLog(@"\n\r\n\r用户当前城市:%@___用户定位城市:%@\n\r\n\r", [CUser DefaultUser].U_CurCity, user.m_AddrInfo.m_strCity);
        user.U_CurCity = [NSString stringWithFormat:@"%@",user.m_AddrInfo.m_strCity];
        [[CDBManager ScheduledShareInstance] ClearTable:@"Best"];
        [[CDBManager ScheduledShareInstance] ClearTable:@"Nearest"];
        [[CDBManager ScheduledShareInstance] ClearTable:@"Newest"];
        [[CDBManager ScheduledShareInstance] ClearTable:@"Commentaries"];
        [[AppConfig DefaultAppConfig] RemovePathInApp:@"Documents" SubPath:@"DeliciousImages"];
        [[AppConfig DefaultAppConfig] CreatePathInApp:@"Documents" SubPath:@"DeliciousImages"];
        
        //CKey *key =[[CKey alloc] initWithType:3 Value:user.U_CurCity Name:@"U_CurCity"];
        //[CUser UpadteUserInfo:[NSString stringWithFormat:@"U_AID=%lld", [user.U_AID longLongValue]] Keys:[NSArray arrayWithObjects:key, nil]];
        //[key release];
    }
}

-(void)LoadDatas
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    if (!m_Ctrls)
    {
        m_Ctrls  = [[NSMutableArray array] retain];
        
        //mainpage
        [CMainPageCtrl DefaultMainPageCtrl].title = @"浏览";
        [self.view addSubview:[CMainPageCtrl DefaultMainPageCtrl].view];
        [CMainPageCtrl DefaultMainPageCtrl].view.tag = 0;
        
        //uploadpage
        [CUploadPageCtrl DefaultUploadPage].title = @"美食上传";
        [self.view addSubview:[CUploadPageCtrl DefaultUploadPage].view];
        [CUserPageCtrl DefaultUserPage].view.tag = 1;
        
        //favoritepage
        [CFavoriteCtrlPage DefaultFavoritePage].title = @"我的收藏";
        [self.view addSubview:[CFavoriteCtrlPage DefaultFavoritePage].view];
        [CFavoriteCtrlPage DefaultFavoritePage].view.tag = 2;
        
        //searchpage
        [CSearchPageCtrl DefaultSearchPage].title = @"搜索";
        [CSearchPageCtrl DefaultSearchPage].view.alpha = 0.0;
        [self.view addSubview:[CSearchPageCtrl DefaultSearchPage].view];
        [CSearchPageCtrl DefaultSearchPage].view.tag = 3;
        
        //userpage
        [CUserPageCtrl DefaultUserPage].title = @"";
        [self.view addSubview:[CUserPageCtrl DefaultUserPage].view];
        [CUserPageCtrl DefaultUserPage].view.tag = 4;
        
        [self.view bringSubviewToFront:[CMainPageCtrl DefaultMainPageCtrl].view];
        
        //share
        //m_Share = [[[NSBundle mainBundle] loadNibNamed:@"ShareView" owner:self options:nil] objectAtIndex:0];
        //m_Share.alpha = 0.0;
        //m_Share.delegate = [CMainPageCtrl DefaultMainPageCtrl];
        //[self.view addSubview:m_Share];
        
        //commentary
        m_ComEdit = [[CComEditView alloc] initWithFrame:CGRectMake(-self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];
        m_ComEdit.delegate = [CMainPageCtrl DefaultMainPageCtrl];
        [self.view addSubview:m_ComEdit];
        [m_ComEdit release];
        
        //imageselector
        //m_ImageSelector = [[[NSBundle mainBundle] loadNibNamed:@"ImageSelector" owner:self options:nil] objectAtIndex:0];
        //m_ImageSelector.alpha = 0.0;
        //[self.view addSubview:m_ImageSelector];
        
        m_TabBar = [[UITabBarEx alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-58, self.view.frame.size.width, 58)];
        m_TabBar.delegate = self;
        [self.view addSubview:m_TabBar];
        [m_TabBar release];
        
        CTabBarItem *item1 = [[CTabBarItem alloc] initWithImageSourcePath:@"preview.png" SelectedPath:@"previewh.png" Text:@"我的浏览"];
        [item1 setTag:0];
        
        CTabBarItem *item2 = [[CTabBarItem alloc] initWithImageSourcePath:@"upload.png" SelectedPath:@"uploadh.png" Text:@"美食上传"];
        [item2 setTag:1];
        
        CTabBarItem *item3 = [[CTabBarItem alloc] initWithImageSourcePath:@"myfood.png" SelectedPath:@"myfoodh.png" Text:@"我的美食"];
        [item3 setTag:2];
        
        [m_TabBar SetItems:[NSArray arrayWithObjects:item1, item2, item3, nil]];
        [m_TabBar SetSelectedItem:item1];
        [m_TabBar SetItemHight:item1 IsHight:NO];
        
        [item1 release];
        [item2 release];
        [item3 release];
        
        [self.view bringSubviewToFront:m_TabBar];
        [self.view bringSubviewToFront:m_ComEdit];
        [self.view bringSubviewToFront:m_Share];
        //[self.view bringSubviewToFront:m_ImageSelector];
        [self.view bringSubviewToFront:m_OverView];
        [self.view bringSubviewToFront:m_LoadView];
        
        if (m_bIsAlterShow)
        {
            [m_AlterView dismissWithClickedButtonIndex:0 animated:YES];
            m_AlterView = nil;
        }
    }
    else if (!m_bIsActive && ![m_Ctrls count])
    {
        //NSLog(@"请求服务器超时或者错误，继续请求。");
        [[CMainPageCtrl DefaultMainPageCtrl] TabBestAction];
    }
    
    [pool release];
}

//overlayer
-(void)RemoveCover
{
    if (!self.m_bIsActive && 1.0000 == m_OverView.alpha)
    {
        m_bIsActive = YES;
        
        if (m_bIsOver)
        {
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:1.0];
            /*[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
            [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:m_OverView cache:YES];
            [UIView setAnimationDelegate:self];
            [UIView setAnimationDidStopSelector:@selector(DidRemoveCover)];*/
            m_OverView.alpha = 0.0;
            m_OverView.transform = CGAffineTransformMakeScale(1.5, 1.5);
            [UIView commitAnimations];
        }
    }
}

-(void)DidRemoveCover
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MSG_NETWORK_STATE object:nil];
    
    //m_CurPage.image = nil;
    //m_NextPage.image= nil;
    m_Cover.image = nil;
    m_Animas.image= nil;
}

-(void)ChangeCover
{    
    srandom(time(0));
    int value = random() % 10 + 1;
    if (m_nImageNo == value)
    {
        value = (arc4random() % 10) + 1;
    }
    m_nImageNo = value;
    
    m_NextPage.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", m_nImageNo]];
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.5];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(DidChangeCover)];
    m_CurPage.alpha = 0.0;
    m_NextPage.alpha= 1.0;
    [UIView commitAnimations];
}

-(void)DidChangeCover
{
    UIImageView *temp = m_NextPage;
    m_NextPage = m_CurPage;
    m_CurPage = temp;
    m_NextPage.image = nil;
    
    if (!m_bIsActive)
    {
        [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(ChangeCover) userInfo:nil repeats:NO];
    }
    else 
    {
        [self DidRemoveCover];
    }
}

-(void)AnimationOver
{
    m_Animas.animationImages = nil;
    m_bIsOver = YES;
    
    if (m_bIsActive)
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1.0];
        /*[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
         [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:m_OverView cache:YES];
         [UIView setAnimationDelegate:self];
         [UIView setAnimationDidStopSelector:@selector(DidRemoveCover)];*/
        m_OverView.alpha = 0.0;
        m_OverView.transform = CGAffineTransformMakeScale(1.5, 1.5);
        [UIView commitAnimations];
    }
}

-(void)SortSubviews:(UIView*)view
{
    [self.view bringSubviewToFront:view];
    if ([CMainPageCtrl DefaultMainPageCtrl].view == view)
    {
        [CMainPageCtrl DefaultMainPageCtrl].m_bIsHidden = NO;
    }
    else 
    {
        [CMainPageCtrl DefaultMainPageCtrl].m_bIsHidden = YES;
    }
    
    //[self.view bringSubviewToFront:m_TabView];
    [self.view bringSubviewToFront:m_TabBar];
    [self.view bringSubviewToFront:m_ComEdit];
    //[self.view bringSubviewToFront:m_Share];
    //[self.view bringSubviewToFront:m_ImageSelector];
    [self.view bringSubviewToFront:m_OverView];
    [self.view bringSubviewToFront:m_LoadView];
}

-(void)BringCtrlToFront:(int)index
{
    //show mainpage
    if (0 == index && [m_TabBar GetProCursor] != index)
    {
        [self SortSubviews:[CMainPageCtrl DefaultMainPageCtrl].view];
        if (![CMainPageCtrl DefaultMainPageCtrl].m_Map.m_bIsShow)
            [m_TabBar SetItemHight:[m_TabBar.m_Items objectAtIndex:0] IsHight:NO];
        else 
            [m_TabBar SetItemHight:[m_TabBar.m_Items objectAtIndex:0] IsHight:YES];
        if (2 == [m_TabBar GetProCursor])
        {
            [[CUserPageCtrl DefaultUserPage] UserPageHidden];
        }
    }
    else if (0 == index && ![CMainPageCtrl DefaultMainPageCtrl].m_Map.m_bIsShow)
    {
        [self SortSubviews:[CMainPageCtrl DefaultMainPageCtrl].view];
        [[CMainPageCtrl DefaultMainPageCtrl] ShowMapView];
        [m_TabBar SetItemHight:[m_TabBar.m_Items objectAtIndex:0] IsHight:YES];
        if (2 == [m_TabBar GetProCursor])
        {
            [[CUserPageCtrl DefaultUserPage] UserPageHidden];
        }
    }
    //show uploadpage
    else if (1 == index /*&& [m_TabBar GetProCursor] != index*/)
    {
        if (Logined == [CUser DefaultUser].m_UType)
        {
            [self SortSubviews:[CUploadPageCtrl DefaultUploadPage].view];
            [[CUploadPageCtrl DefaultUploadPage] ShowUploadPageCtrl];
            /*if (2 == [m_TabBar GetProCursor])
            {
                if (3 == [CUserPageCtrl DefaultUserPage].m_nIndex)
                {
                    [[CUserPageCtrl DefaultUserPage] UserPageHidden];
                }
            }*/
        }
        else 
        {
            /*UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请先登陆" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:nil];
            alter.tag = 110;
            [alter show];
            [alter autorelease];*/
            
            [[CRootCtrlManager DefaultRootCtrl].m_TabBar DidTabBarItemSelected:[[CRootCtrlManager DefaultRootCtrl].m_TabBar.m_Items objectAtIndex:2]];
        }
    }
    //show my foods
    else if (2 == index && [m_TabBar GetProCursor] != index)
    {
        [self SortSubviews:[CUserPageCtrl DefaultUserPage].view];
        [[CUserPageCtrl DefaultUserPage] DidOptionPageCtrlShown];
    }
    //show searchview
    else if (3 == index)
    {
        [self SortSubviews:[CSearchPageCtrl DefaultSearchPage].view];
        [[CSearchPageCtrl DefaultSearchPage] ShowSearchPageCtrl];
    }
    else if (-1 == index)
    {
        [self.view sendSubviewToBack:[CUploadPageCtrl DefaultUploadPage].view];
        [m_TabBar SetSelectedItem:[m_TabBar.m_Items objectAtIndex:[m_TabBar GetProCursor]]];
        /*if (2 == [m_TabBar GetCursor])
        {
            [[CUserPageCtrl DefaultUserPage].m_MyFoodView SetInfoViewInfo];
        }*/
    }
    else {;}
}

-(IBAction)OnButtonDown:(UIButton*)sender
{   
     m_TbFocus.center = CGPointMake(sender.center.x, m_TbFocus.center.y);
    
    [self BringCtrlToFront:sender.tag];
}

-(void)TabBarEx:(UITabBarEx *)tabbar TabBarItem:(CTabBarItem *)tabbaritem
{
    [self BringCtrlToFront:tabbaritem.tag];
}

//focusview
-(void)ShowCurView:(int)index
{
    UIButton *btn = nil;
    
    //mainpage
    if (0 == index)
    {
        btn = m_PreViewBtn;
    }
    
    [self OnButtonDown:btn];
}

-(void)ShowSubView:(UIView*)subview
{
    if (0.0 == subview.alpha && subview)
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.1];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(DidSubViewShowed)];
        //CGPoint point = CGPointMake(self.view.frame.size.width/2, subview.center.y);
        //subview.center = point;
        subview.alpha   = 1.0;
        [UIView commitAnimations];
    }
}

-(void)DidSubViewShowed
{
    //upload
    if (3 == [m_TabBar GetCursor])
    {
        [[CUploadPageCtrl DefaultUploadPage] Photograph];
    }
}

-(void)HiddenSubView:(UIView*)subview
{
    /*if (1.0 == subview.alpha && subview)
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        subview.alpha = 0.0;
        [UIView commitAnimations];
    }*/
    if (!subview.hidden)
    {
        subview.hidden = YES;
        [CMainPageCtrl DefaultMainPageCtrl].m_TopBarView.hidden = NO;
    }
}

-(void)DidSubViewHidden
{
    //
}

//loadview
-(void)ShowLoadView:(CGFloat)alpha Title:(NSString*)title
{
    m_LoadTitle.text = title;

    if (0.0 == m_LoadView.alpha)
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1.0];
        m_LoadBg.alpha   = alpha;
        [m_LoadIndicator startAnimating];
        m_LoadView.alpha = 1.0;
        [UIView commitAnimations];
    }
}

-(void)SetLoadViewTitle:(NSString*)title
{
    m_LoadTitle.text = title;
}

-(void)HiddenLoadView
{
    if (1.0 == m_LoadView.alpha)
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1.0];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(DidFinishLoad)];
        m_LoadView.alpha = 0.0;
        [UIView commitAnimations];
    }
}

-(void)DidFinishLoad
{
    [m_LoadIndicator stopAnimating];
}

//share
-(void)ShowShareView
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(DidShareShow)];
    m_Share.alpha = 1.0;
    [UIView commitAnimations];
}
-(void)DidShareShow
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    m_Share.m_PanelView.center = CGPointMake(m_Share.m_PanelView.center.x, m_Share.m_PanelView.center.y-m_Share.m_PanelView.frame.size.height-18);
    [UIView commitAnimations];
}

-(void)HiddenShareView
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(DidShareHidden)];
    m_Share.m_PanelView.center = CGPointMake(m_Share.m_PanelView.center.x, m_Share.m_PanelView.center.y+m_Share.m_PanelView.frame.size.height+18);
    [UIView commitAnimations];
}

-(void)DidShareHidden
{
    [UIView beginAnimations:nil context:nil];
    m_Share.alpha = 0.0;
    [UIView commitAnimations];
}

//commentary
-(void)ShowComEditView
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    //[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(ComEditViewShown)];
    [CMainPageCtrl DefaultMainPageCtrl].m_bIsHidden = YES;
    m_ComEdit.center = CGPointMake(self.view.frame.size.width/2, m_ComEdit.center.y);
    [UIView commitAnimations];
}

-(void)ComEditViewShown
{
    [m_ComEdit CommentariesWorker:m_ComEdit.m_Product];
}

-(void)HiddenComEditView
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    //[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(ComEditViewHidden)];
    m_ComEdit.center = CGPointMake(-self.view.frame.size.width/2, m_ComEdit.center.y);
    [UIView commitAnimations];
}

-(void)ComEditViewHidden
{
    [CMainPageCtrl DefaultMainPageCtrl].m_bIsHidden = NO;
    [m_ComEdit ResetComEditView];
}

//imageseletor
-(void)ShowImageSelector
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDidStopSelector:@selector(DidImageSelectorShown)];
    m_ImageSelector.alpha = 1.0;
    [UIView commitAnimations];
}

-(void)HiddenImageSelector
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(DidImageSelectorHidden)];
    [UIView setAnimationDuration:0.3];
    m_ImageSelector.m_SelectorView.center = CGPointMake(m_ImageSelector.m_SelectorView.center.x, m_ImageSelector.m_SelectorView.center.y+m_ImageSelector.m_SelectorView.frame.size.height);
    [UIView commitAnimations];
}

-(void)DidImageSelectorShown
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    m_ImageSelector.m_SelectorView.center = CGPointMake(m_ImageSelector.m_SelectorView.center.x, m_ImageSelector.m_SelectorView.center.y-m_ImageSelector.m_SelectorView.frame.size.height);
    [UIView commitAnimations];
}

-(void)DidImageSelectorHidden
{
    [UIView beginAnimations:nil context:nil];
    m_ImageSelector.alpha = 0.0;
    [UIView commitAnimations];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

//show alterview
-(void)ShowAlterView:(NSString*)mes State:(int)state
{
    if (!m_bIsAlterShow)
    {
        //定位，有网，城市无数据，无定位处理，获取世界数据(启动页面)
        if (3 == state)
        {
            //NSLog(@"\r\n\r\n启动选择的城市:%@没啥吃的\r\n\r\n", [CLocator DefaultLocator].m_AddrInfo.m_strCity);
            m_bIsAlterShow = YES;
            NSString *strcity = [NSString stringWithFormat:@"%@", [CLocator DefaultLocator].m_AddrInfo.m_strCity];
            [CLocator DefaultLocator].m_AddrInfo.m_strCity = nil;
            [[CMainPageCtrl DefaultMainPageCtrl] FreshMianPage:NO];
            [CLocator DefaultLocator].m_AddrInfo.m_strCity = strcity;
            
            return;
        }
        //程序启动获取数据错误
        else if (1 == state)
        {
            UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示-RootView" message:mes delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:nil];
            m_AlterView = alter;
            alter.tag = 3;
            [alter show];
            [alter autorelease];
            
            m_bIsAlterShow = YES;
        }
        //程序启动获取数据超时
        else if (2 == state)
        {
            UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示-RootView" message:mes delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:nil];
            m_AlterView = alter;
            alter.tag = 3;
            [alter show];
            [alter autorelease];
            
            m_bIsAlterShow = YES;
        }
        //无网络本地没有缓存
        else if (0 == state)
        {
            if (!m_bIsAlterShow)
            {
                UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示-RootView" message:mes delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:nil];
                m_AlterView = nil;
                alter.tag = 3;
                [alter show];
                [alter autorelease];
            }
            
            m_bIsAlterShow = YES;
        }
        else {;}
        
        //app未启动处于启动页面，检测网络状态
        if (!m_bIsActive)
        {
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(NetWorkRun:) name:MSG_NETWORK_STATE object:nil];
        }
    }
}

//self
-(void)DidAppBecameActive:(NSNotification*)notification
{
    //启动检查网络
    [[AppConfig DefaultAppConfig] CheckNetwork];
    
    //获取语言
    [[AppConfig DefaultAppConfig] GetOSCurLanguage];
    //获得当前登陆用户
    [[CUser DefaultUser] GetCurUser];

    //获取定位以及网络状况
    LocalRet localret = [[CLocator DefaultLocator] CheckDevLocation];
    //没有网络
    if (NetNotSupport == localret)
    {
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"没有可用网络定位功能无法使用，请检查网络。" delegate:self cancelButtonTitle:@"退出" otherButtonTitles:@"继续", nil];
        alter.tag = 1;
        [alter show];
        [alter autorelease];
    }
    else if (DevNotSupport == localret)
    {
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"设备未打开或者不支持定位功能。" delegate:self cancelButtonTitle:@"退出" otherButtonTitles:@"继续", nil];
        alter.tag = 1;
        [alter show];
        [alter autorelease];
    }
    //未打开定位使用权限
    else if (AuthNotSupport == localret)
    {
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"系统没有权限使用定位功能，请在设置中打开。" delegate:self cancelButtonTitle:@"退出" otherButtonTitles:@"继续", nil];
        alter.tag = 1;
        [alter show];
        [alter autorelease];
    }
    //能够正常定位，等待定位结果刷新主页，主页如果是可视的
    else if (NormalSupport == localret)
    {
        [[CLocator DefaultLocator] StartLocation];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(LocationReponse:) name:MSG_LOCATION_RET object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(LocationReponse:) name:MSG_LOCATION_TIMEOUT object:nil];
    }
    else 
    {
        ;//[self LoadDatas];
    }
}

-(void)DidAppEnterBackground:(NSNotification*)notification
{
    //停止检查网络
    [AppConfig DefaultAppConfig].m_bIsCheckNet = NO;
    //停止发送心跳，若存在
    [[CUser DefaultUser] StopHeartBeat];
    //取消对网络和定位的监听
    [[NSNotificationCenter defaultCenter] removeObserver:[CRootCtrlManager DefaultRootCtrl] name:MSG_NETWORK_STATE object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:[CRootCtrlManager DefaultRootCtrl] name:MSG_LOCATION_RET object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:[CRootCtrlManager DefaultRootCtrl] name:MSG_LOCATION_TIMEOUT object:nil];
    
    if (m_CitySel)
    {
        [m_CitySel removeFromSuperview];
    }
}

-(void)DidCitySelectorActived:(int)index Object:(id)object
{
    //用户位置发生变化，清空缓存,启动页面
    if (!m_bIsActive)
    {
        if ([m_CitySel.m_City.text length] > 6)
        {
            m_CitySel.alpha = 0.0;
            //设置用户数据缓存
            [self SetUserData:YES];
            //开始加载主页数据
            [self performSelector:@selector(LoadDatas) withObject:self afterDelay:1];
            [m_CitySel removeFromSuperview];
            m_CitySel = nil;
        }
        else 
        {
            UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请您告诉我要去的地方..." delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil];
            [alter show];
            [alter autorelease];
        }
    }
}

//city
-(void)ShowCitySelView
{
    m_CitySel = [[CCitySelector alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) ShowGrade:ToCity];
    //[m_CitySel.m_NaviItem setTitle:@"想去的城市"];
    //m_CitySel.m_Title.text = @"想去的城市";
    //m_CitySel.m_Title.font = [UIFont boldSystemFontOfSize:18];
    m_CitySel.alpha = 0.0;
    m_CitySel.m_NaviItem.leftBarButtonItem = nil;
    m_CitySel.delegate = self;
    [self.view addSubview:m_CitySel];
    [m_CitySel release];
    m_CitySel.alpha = 1.0;
    
    //hchk-test-start
    //[self LoadDatas];
    //[self RemoveCover];
    //hchk-test-end
}

//uialterview
//- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (1 == alertView.tag)
    {
        if (0 == buttonIndex)
        {
            //NSLog(@"<========程序退出========>");
            exit(0);
        }
        else if (1 == buttonIndex)
        {
            if (![CRootCtrlManager DefaultRootCtrl].m_bIsActive)
            {
                //不能或者用户不容许定位，强制用户选择城市
                //[self ShowCitySelView];
                //设置用户数据缓存
                [self SetUserData:YES];
                //开始加载主页数据
                [self performSelector:@selector(LoadDatas) withObject:self afterDelay:1];
            }
        }
        else {;}
    }
    else if (2 == alertView.tag)
    {
        exit(0);
    }
    else if (3 == alertView.tag)
    {
        m_AlterView = nil;
        m_bIsAlterShow = NO;
    }
    //没有网路
    else if (100 == alertView.tag)
    {
        [self performSelector:@selector(LoadDatas) withObject:nil afterDelay:1];
    }
    //没有登陆上传
    else if (110 == alertView.tag)
    {
        [m_TabBar SetSelectedItem:[m_TabBar.m_Items objectAtIndex:[m_TabBar GetProCursor]]];
    }
    else {;}
}

@end

